/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package intelligenttm.SourceFile;

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 望舒凌霄
 */
public class ScanFileAndFolder {
    private List FilePath = new ArrayList<>();
    private List FolderPath = new ArrayList<>();
    private File SelectedFile = null;
    
    //构造函数
    public ScanFileAndFolder() {
    }

    public ScanFileAndFolder(String FP) {
        SelectedFile=new File(FP);
    }

    public ScanFileAndFolder(File F) {
        SelectedFile=F;
    }

    public ScanFileAndFolder(URI uri) {
        SelectedFile=new File(uri);
    }
    
    //获取File和Floder路径与名称
    public List getFilePath() {
        return FilePath;
    }

    public List getFolderPath() {
        return FolderPath;
    }
    
    //设置选择文件或文件夹路径
    public void setSelectedFile(File SelectedFile) {
        this.SelectedFile = SelectedFile;
    }
    
    public void setSelectedFile(String FP) {
        this.SelectedFile = new File(FP);
    }
    
    public void setSelectedFile(URI uri) {
        this.SelectedFile = new File(uri);
    }
    
    public void ScanCurrentFolder(){
        if(SelectedFile.isDirectory()){
            File[] NowFile=SelectedFile.listFiles();
            for (File NowFile1 : NowFile) {
                if (NowFile1.isFile()) {
                    FilePath.add(NowFile1.getPath());
                } else if (NowFile1.isDirectory()) {
                    FolderPath.add(NowFile1.getPath());
                }
            }
        }
        else if(SelectedFile.isFile()){
            FilePath.add(SelectedFile.getPath());
            FolderPath.add(SelectedFile.getParentFile().getPath());
        }
    }
    
    public void ScanCurrentFolder(String Type){
        if(SelectedFile.isDirectory()){
            File[] NowFile=SelectedFile.listFiles();
            for (File NowFile1 : NowFile) {
                if (NowFile1.isFile()) {
                    String FileName=NowFile1.getName();
                    int i = FileName.lastIndexOf(".");
                    if(i>-1 && i<FileName.length()){
                        String Name = FileName.substring(0, i); //--文件名
                        String Extention = FileName.substring(i+1); //--扩展名
                        if(Extention==Type){
                            FilePath.add(NowFile1.getPath());
                        }
                    }
                } else if (NowFile1.isDirectory()) {
                    FolderPath.add(NowFile1.getPath());
                }
            }
        }
        else if(SelectedFile.isFile()){
            String FileName=SelectedFile.getName();
            int i = FileName.lastIndexOf(".");
            if(i>-1 && i<FileName.length()){
                String Name = FileName.substring(0, i); //--文件名
                String Extention = FileName.substring(i+1); //--扩展名
                if(Extention==Type){
                    FilePath.add(SelectedFile.getPath());
                    FolderPath.add(SelectedFile.getParentFile().getPath());
                }
            }
        }
    }
    
    public void ScanAll(){
        if (SelectedFile.isFile()){
            FilePath.add(SelectedFile.getPath());
            FolderPath.add(SelectedFile.getParentFile().getPath());
        }
        else if(SelectedFile.isDirectory()){
            ScanAllFile(SelectedFile,FilePath,FolderPath);
        }
    }
    
    public void ScanAll(String Type){
        if (SelectedFile.isFile()){
            String FileName=SelectedFile.getName();
            int i = FileName.lastIndexOf(".");
            if(i>-1 && i<FileName.length()){
                String Name = FileName.substring(0, i); //--文件名
                String Extention = FileName.substring(i+1); //--扩展名
                if (Extention==Type){
                    FilePath.add(SelectedFile.getPath());
                    FolderPath.add(SelectedFile.getParentFile().getPath());
                }
            }
        }
        else if(SelectedFile.isDirectory()){
            ScanAllFile(SelectedFile,FilePath,FolderPath,Type);
        }
    }
    
    public void ScanAllFile(File NowFile,List NewFileList,List NewFolderList){
        if(NowFile.isDirectory()){
            File[] F = NowFile.listFiles();
            for (File F1 : F) {
                if (F1.isFile()) {
                    NewFileList.add(F1.getPath());
                } else if (F1.isDirectory()) {
                    NewFolderList.add(F1.getPath());
                    ScanAllFile(F1, NewFileList, NewFolderList);
                }
            }
        }
    }
    
    public void ScanAllFile(File NowFile,List NewFileList,List NewFolderList,String Type){
        if(NowFile.isDirectory()){
            File[] F = NowFile.listFiles();
            for (File F1 : F) {
                if (F1.isFile()) {
                    String FileName=F1.getName();
                    int i = FileName.lastIndexOf(".");
                    if(i>-1 && i<FileName.length()){
                        String Name = FileName.substring(0, i); //--文件名
                        String Extention = FileName.substring(i+1); //--扩展名
                        if (Extention==Type){
                            NewFileList.add(F1.getPath());
                        }
                    }
                } else if (F1.isDirectory()) {
                    NewFolderList.add(F1.getPath());
                    ScanAllFile(F1, NewFileList, NewFolderList,Type);
                }
            }
        }
    }
}
